package com.buss.base.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.base.dao.BranchDao;
import com.buss.base.model.Branch;
import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;

@Repository
public class BranchDaoImpl implements BranchDao {

	@Autowired
	private BaseDao<Branch> baseDao;
	
	@Override
	public Branch saveBranch(Branch branch) {
		return baseDao.saveOrEdit(branch);
	}

	@Override
	public void editBranch(Branch branch) {
		baseDao.edit(branch);

	}

	@Override
	public void deleteBranch(Long[] ids) {
		String sql = "delete from Branch branch where branch.id in (:ids)";
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}

	@Override
	public Branch loadBranch(Long id) {
		return baseDao.get(id, Branch.class);
	}

	public void updateStatus(Long[] ids,RecordStatus status){
		String sql = "update Branch branch set branch.status = :status where branch.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", status);
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}
	
	@Override
	public ResultList<Branch> queryBranch(String companyId, String name,
			String pinyin,RecordStatus status, Paginalbe page, OrderBy orderBy) {
		String sql = "select branch from Branch branch where branch.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		if(StringUtil.isNotEmpty(name)){
			sql += " and branch.name like :name ";
			params.put("name", "%" + name + "%");
		}
		if(StringUtil.isNotEmpty(pinyin)){
			sql += " and branch.pinyin like :pinyin or branch.pinyinHead like :pinyin ";
			params.put("pinyin", "%" + pinyin + "%");
		}
		if(status != null){
			sql += " and branch.status = :status ";
			params.put("status", status);
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("addDate", OrderBy.DESC);
		}
		sql += orderBy.aggOrder("branch");
		return baseDao.queryHsql(sql, params, page);
	}

	public ResultList<Branch> queryBranch(String companyId,Long[] branchIds){
		String sql = "select branch from Branch branch where branch.companyId = :companyId " +
				"and branch.id in (:branchIds) ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("branchIds", branchIds);
		return baseDao.queryHsql(sql, params, null);
	}
	
	public ResultList<Branch> queryBranchByRole(Long[] roleIds,RecordStatus status,Paginalbe page){
		String sql = "select distinct branch from Branch branch inner join branch.roles role " +
				" where role.id in (:roleIds) ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleIds", roleIds);
		if(status != null){
			sql += " and branch.status = :status ";
			params.put("status", status);
		}		
		return baseDao.queryHsql(sql, params, page);
	}
	
	public void saveBranchRole(Long roleId,Long[] branchIds){
		String sql = "delete from base_role_branch where roleId = :roleId and branchId in (:branchIds)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		params.put("branchIds", branchIds);
		baseDao.excuteNative(sql, params);
		sql = "insert into base_role_branch(roleId,branchId) values(:roleId,:branchId)";
		for(Long branchId : branchIds){
			params = new HashMap<String, Object>();
			params.put("roleId", roleId);
			params.put("branchId", branchId);
			baseDao.excuteNative(sql, params);
		}
	}
	
	public void saveCancelBranch(Long roleId,Long[] branchIds){
		String sql = "delete from base_role_branch where roleId = :roleId and branchId in (:branchIds)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("roleId", roleId);
		params.put("branchIds", branchIds);
		baseDao.excuteNative(sql, params);
	}
}
